import { Part } from '@/api/interface/part/inventory';
import { customTable } from '@/components/ProTable/interface';
import { ProTableValue } from '@/interface/public';

/**
 * @name 配件清单
 */
export function useInventoryData(inventoryDataRef: ProTableValue, dataGridRef: ProTableValue) {
  let inventoryData = reactive<customTable>({
    initParam: {
      status: 0
    },
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'left', width: 160 },
      {
        label: '名称',
        prop: 'name',
        search: {
          label: '关键字',
          key: 'key',
          el: 'input',
          props: {
            placeholder: '请输入关键字'
          }
        }
      },
      {
        label: '单位',
        prop: 'unit'
      },
      {
        label: '标准件标识',
        prop: 'standardType',
        required: true
      },
      {
        label: '规格',
        prop: 'size'
      },
      {
        label: '库存数',
        prop: 'quantity'
      },
      {
        label: '安全库存量',
        prop: 'minQuantity'
      },
      {
        label: '状态',
        prop: 'status',
        tag: true,
        enum: enableOrDisabledStatus
      },
      {
        label: '备注',
        prop: 'remark'
      }
    ],
    headButtonList: [],
    rowButtonList: [
      {
        label: '选择',
        type: 'primary',
        attrs: {
          link: true
        },
        methods: (row: Part.ResSearchProp) => {
          selectInventoryData(row);
        }
      }
    ]
  });

  /**
   * @description: 打开设备选择弹框
   * @return {*}
   */
  const openInventoryData = () => {
    inventoryData.visible = true;
    nextTick(() => {
      inventoryDataRef.value?.getTableList();
    });
  };
  /**
   * @description: 选择配件
   * @return {*}
   */
  const selectInventoryData = (row: Part.ResSearchProp) => {
    const tableRow = dataGridRef.value?.tableData.find((item: any) => item.isAddFlag || item['edit']);
    tableRow.partInfo = row.partInfo;
    tableRow.partId = row.id;
    inventoryData.visible = false;
  };

  return {
    inventoryData,
    selectInventoryData,
    openInventoryData
  };
}
